package cn.christ.com.mobilesafe.view.activity_lockscreen;

import android.app.Activity;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import cn.christ.com.mobilesafe.R;
import cn.christ.com.mobilesafe.engine.AppProvider;
import cn.christ.com.mobilesafe.modle.AppInfo;

/**
 * @author U 程序锁屏的UI
 */
public class LockScreenActivity extends Activity {
	public static final String ACTION_FREE_DOG = "org.christ.dog.free";
	private TextView mTvLockAppName;
	private Button mBtnLockApp;
	private EditText mEtPw;
	private AppInfo mAppInfo;
	private Drawable mIcon;
	private String mPackageName;
	// 锁屏的Activity ok
	// UI ok
	// data（packageName）--AppInfo ok
	// 对事件进行处理：非空校验，判断密码是否正确--正确---finish---发个广播通知看门狗放行 ok
	// 处理返回键（禁用）
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		initView();
		initData();
		initEvent();

	}

	private void initView() {
		setContentView(R.layout.activity_lock_screen);
		mTvLockAppName = (TextView) findViewById(R.id.tv_lock_app_name);
		mBtnLockApp = (Button) findViewById(R.id.btn_lock_app);
		mEtPw = (EditText) findViewById(R.id.et_lock_app_pwd);
	}

	private void initData() {
		mPackageName = getIntent().getStringExtra("packageName");
		// 1 通过包名获取AppInfo
		mAppInfo = AppProvider.getAllAppInfo(getApplicationContext(), mPackageName);
		mTvLockAppName.setText(mAppInfo.name);
		mIcon = mAppInfo.icon;
		mIcon.setBounds(0, 0, mIcon.getMinimumWidth(), mIcon.getMinimumHeight());
		mTvLockAppName.setCompoundDrawables(null, mIcon, null, null);
	}

	private void initEvent() {
		mBtnLockApp.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				String psw = mEtPw.getText().toString();
				if(TextUtils.isEmpty(psw)){
					Toast.makeText(getApplicationContext(), "密码不能为空", 0).show();
					return;
				}
				if("123".equals(psw)){
					finish();
					//通知看门狗，放过他
					// 发个广播
					Intent intent=new Intent();
					intent.putExtra("packageName", mPackageName);
					intent.setAction("org.christ.watchdog.free");
					sendBroadcast(intent);
				}
			}
		});
		
	}

	// 禁用返回键
	@Override
	public void onBackPressed() {

	}

	// // 禁用返回键
	// @Override
	// public boolean onKeyDown(int keyCode, KeyEvent event) {
	// if(keyCode==KeyEvent.KEYCODE_BACK){// 接收到返回键是，啥不处理，相当于禁用
	//// Toast.makeText(getApplicationContext(), "姐们不要误操作", 0).show();
	// return true;
	// }
	// return super.onKeyDown(keyCode, event);
	// }

}
